package com.enqualcomm.kids.mvp.pedometer;

import com.android.volley.NetworkResponse;
import com.android.volley.VolleyError;
import com.android.volley.socket.SocketClient;
import com.android.volley.socket.SocketNetwork;
import com.enqualcomm.kids.component.MyApplication;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.TerminalDefault;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.Parser;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.QueryStepCountParams;
import com.enqualcomm.kids.network.socket.request.QueryStepDayListParams;
import com.enqualcomm.kids.network.socket.response.BasicResult;
import com.enqualcomm.kids.network.socket.response.QueryStepCountResult;
import com.enqualcomm.kids.network.socket.response.QueryStepDayListResult;

import java.text.SimpleDateFormat;
import java.util.Date;

import common.utils.NetUtil;

/**
 * Created by yinzhiqun on 2015/10/27.
 *
 */
public class PedometerCountModel extends NetworkModel implements IPedometerCountModel{
    private final SocketNetwork socketNetwork;
    private final AppDefault appDefault;
    QueryStepDayListResult daylist;

    public PedometerCountModel() {
        appDefault = new AppDefault();
        socketNetwork = SocketClient.initSocketNetwork(MyApplication.getInstance());
    }

    @Override
    public void getPedometerCount(String terminalid, PedometerHandler handler) {
        if (NetUtil.checkNet(MyApplication.getInstance())){
            QueryStepCountParams params = new QueryStepCountParams(appDefault.getUserkey(), terminalid);
            try {
                NetworkResponse networkResponse = socketNetwork.performRequest(new SocketRequest<BasicResult>(params, null));
                QueryStepCountResult stepCountResult = Parser.parse(networkResponse, QueryStepCountResult.class);
                if (stepCountResult.code==0){
                    handler.onGetStepCountSuccess(terminalid,stepCountResult.result.stepcount);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void getDaylistStepCount(final String terminalid, final PedometerHandler handler) {
        String begintime = null;
        String endtime = null;
      final TerminalDefault  terminalDefault = new TerminalDefault(terminalid);
        // "2015-01-20 00:00:00"
        StringBuilder builder = new StringBuilder();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        String dateStr2 = new SimpleDateFormat("yyyy-MM-dd").format(new Date(
                System.currentTimeMillis() - 3600l * 1000 * 24 * 6));
        begintime = builder.append(dateStr2).append(" 00:00:00").toString();
        builder.setLength(0);
        endtime = builder.append(dateStr).append(" 23:59:59").toString();
        QueryStepDayListParams params2 = new QueryStepDayListParams(appDefault.getUserkey(), terminalid, begintime, endtime);
        loadDataFromServer(new SocketRequest<QueryStepDayListResult>(params2, new NetworkListener<QueryStepDayListResult>() {
            @Override
            public void onError(VolleyError error) {
            }

            @Override
            public void onSuccess(QueryStepDayListResult response) {
                if (response.code==0){
                    daylist = response;
                    terminalDefault.setStepDayList(response.result);
                    handler.onGetDayListStepSuccess(terminalid,response);

                }
            }
        }));
    }
}
